
do "E:/ReplicateBuild/02_code/00_environment/00_set_environment.do"

use "$basedata/estimationdata/All_T_J/principal_pref_est_data.dta", clear


** estimate the size of the mistakes principals are making

gen va = va_ma_preY_dev + mu_jt_preY_mean_s_ma
keep if va!=0

keep jtidx schoolidx interview hired offer_declined positive_assessment application_outcome_category va titleI mean_n1_ma mean_n2_ma app_year

gen va_per_student = va/(mean_n1_ma+mean_n2_ma)

gen best_in_pool = va_per_student
gen best_positive_outcome = va_per_student if interview+hired+offer_declined+positive_assessment>0
gen best_interviewed = va_per_student if interview+hired+offer_declined>0
gen best_offered = va_per_student if hired+offer_declined>0

collapse (max) best*, by(jtidx schoolidx app_year titleI)

* compare hired (or offer_declined) vs. best in pool

gen diff_offer_vs_pool = best_in_pool - best_offered

* compare best interviewed or hired (or offer_declined) vs. best in pool

gen diff_interview_vs_pool = best_in_pool - best_interviewed

* compare best positive outcome vs. best in pool

gen diff_positive_vs_pool = best_in_pool - best_positive_outcome

local vars = "diff_positive_vs_pool diff_interview_vs_pool diff_offer_vs_pool"

local i = 0
foreach var in `vars' {
    local i = `i'+1
    qui summ `var', d
	local mean`i' = r(mean)
	local p25`i' = r(p25)
	local median`i' = r(p50)
	local p75`i' = r(p75)
	qui ttest `var', by(titleI)
	local mean`i'_t0 = r(mu_1)
	local mean`i'_t1 = r(mu_2)
	local pval`i' = r(p)
	
	mat a`i' = (`mean`i'',`mean`i'_t0',`mean`i'_t1',`pval`i'',`p25`i'',`median`i'',`p75`i'')
	gen a`i'var = .
}

label var a1var "Applicants vs. Positive Assessment"
label var a2var "Applicants vs. Interview"
label var a3var "Applicants vs. Offer"

forv i=1/3 {
    mat rownames a`i' = a`i'var
}

mat a = a1\a2\a3

frmttable using "$tables/va_mistakes", statmat(a) replace va tex fra ///
	ctitles("","Mean","Mean, non-TI","Mean, TI","non-TI vs TI p value","25th Perc.","50th Perc.","75th Perc.") ///
	sdec(2,2,2,2,2,2,2\2,2,2,2,2,2,2\2,2,2,2,2,2,2) 